
#include <Type\Province.h>

#include "..\Data\User.h"

#include "MultiPostFetcher.h"


namespace TC
{
    namespace Request
    {

        Q_INVOKABLE void MultiPostFetcher::fetchNewer(const QDateTime &time, uint number /*= 10*/)
        {
            if (status() == Status::Communicating)
                return;
            AbstractRemoteProcedure::get("evaluation/multi/fetch/newer",
            {
                { "signature", Data::User::instance()->signature() },
                { "time", time },
                { "number", number }
            });
        }

        Q_INVOKABLE void MultiPostFetcher::fetchOlder(const QDateTime &time, uint number /*= 10*/)
        {
            if (status() == Status::Communicating)
                return;
            AbstractRemoteProcedure::get("evaluation/multi/fetch/older",
            {
                { "signature", Data::User::instance()->signature() },
                { "time", time },
                { "number", number}
            });
        }

        void MultiPostFetcher::onDataReady(QDataStream &stream)
        {
            bool result;
            stream >> result;
            if (result)
            {
                QList<QVariantMap> vms;
                stream >> vms;
                posts.clear();
                for (auto &v : vms)
                {
					v["province"] = TCServer::Type::provinceToString(v.value("province").value<TCServer::Type::Province>());
                    qDebug() << v;
                    posts.append(v);
                }
                setOk();
            }
            else
                setFailed();
        }

    }
}